package edu.zut.cs.network.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.Producer;

import edu.zut.cs.network.entity.Book;

/**
 * 消息生产者-消息发布者（多线程发送）
 * 
 */
public class JMSProducerThread {

	private static final String USERNAME = "yrl"; // 默认的连接用户名
	private static final String PASSWORD = "yrl"; // 默认的连接密码
	private static final String BROKEURL = "tcp://192.168.57.138:61616"; // 默认的连接地址
//	private static final int SENDNUM = 10; // 发送的消息数量
	ConnectionFactory connectionFactory = null; // 连接工厂
	private Connection connection = null;
	private Session session = null;
	private Destination destination = null; // 消息的目的地
	private List<Book> list;
	public JMSProducerThread(List<Book> list) {
		this.list=list;
		// 实例化连接工厂
		connectionFactory=new ActiveMQConnectionFactory(JMSProducerThread.USERNAME, JMSProducerThread.PASSWORD, JMSProducerThread.BROKEURL);
		try {
			connection = connectionFactory.createConnection(); // 通过连接工厂获取连接
			connection.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public void produce() {
		try {
			MessageProducer messageProducer; // 消息生产者
			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建Session
			destination = session.createQueue("sqlqueue1");
			messageProducer = session.createProducer(destination); // 创建消息生产者
//			for (int i = 0; i < JMSProducerThread.SENDNUM; i++) {
//				TextMessage message = session
//						.createTextMessage("ActiveMQ中" + Thread.currentThread().getName() + "线程发送的数据" + ":" + i);
//				System.out.println(Thread.currentThread().getName() + "线程" + "发送消息：" + "ActiveMQ 发布的消息" + ":" + i);
//				messageProducer.send(message);
//				session.commit();
//			}
			 byte[] bs=serialize(list);
		     BytesMessage msg = (BytesMessage) session.createBytesMessage();
		     msg.writeBytes(bs);
		     messageProducer.send(msg);
		     session.commit();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送消息
	 * 
	 * @param session
	 * @param messageProducer
	 * @throws Exception
	 */
//	public static void sendMessage(Session session, MessageProducer messageProducer) throws Exception {
//		for (int i = 0; i < JMSProducerThread.SENDNUM; i++) {
//			TextMessage message = session.createTextMessage("ActiveMQ 发送的消息" + i);
//			System.out.println("发送消息：" + "ActiveMQ 发布的消息" + i);
//			messageProducer.send(message);
//		}
//	}
	public static <T> byte[] serialize(List<T> value) {
		if (value == null) {
			throw new NullPointerException("Can't serialize null");
		}
		byte[] rv=null;
		ByteArrayOutputStream bos = null;
		ObjectOutputStream os = null;
		try {
			bos = new ByteArrayOutputStream();
			os = new ObjectOutputStream(bos);
			for(T obj : value){
				os.writeObject(obj);
			}
			os.writeObject(null);
			os.close();
			bos.close();
			rv = bos.toByteArray();
		} catch (IOException e) {
			throw new IllegalArgumentException("Non-serializable object", e);
		}
		return rv;
	}
}
